package com.compass.doctor.ui.home.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.compass.doctor.databinding.LayoutMenuItemBinding
import com.compass.doctor.databinding.LayoutMineMenuItemBinding
import com.compass.framework.adapter.BaseBindViewHolder
import com.compass.framework.adapter.BaseMultiItemAdapter
import com.compass.framework.ext.saveAs
import com.compass.framework.model.MenuItem
import com.compass.framework.utils.getDrawableResource

/**
 * Description： 菜单adapter
 * Date:2023/12/8 13:47
 */
class MenuAdapter constructor(private var itemType: Int) : BaseMultiItemAdapter<MenuItem>() {

    //用来实现多item布局
    override fun getDefItemViewType(position: Int): Int {
        return itemType
    }

    override fun onBindDefViewHolder(
        holder: BaseBindViewHolder<ViewBinding>,
        item: MenuItem?,
        position: Int
    ) {
        if (item == null) return
        //1、首页菜单， 2、个人中心菜单
        when (holder.itemViewType) {
            1 -> {
                val binding = holder.binding.saveAs<LayoutMenuItemBinding>()
                binding.actionText.text = item?.name
                binding.actionIcon.background = item.resId?.let { getDrawableResource(it) }
            }

            else -> {
                val binding = holder.binding.saveAs<LayoutMineMenuItemBinding>()
                binding.actionText.text = item?.name
                binding.actionIcon.background = item.resId?.let { getDrawableResource(it) }
            }
        }
    }

    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ViewBinding {
        return when (viewType) {
            1 -> {
                LayoutMenuItemBinding.inflate(layoutInflater, parent, false)
            }

            else -> {
                LayoutMineMenuItemBinding.inflate(layoutInflater, parent, false)
            }
        }

    }
}